package com.fit.pages;

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.enums.AccountRole;
import com.fit.interfaces.User;
import com.fit.service.Users;

public class EditUser {

	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}
	
	@Persist
	@Property
	private User currentUser;
	
	@Inject
	private Users users;
	
	Object onActivate(){
		if(!userExists) return Login.class;
		if(user.getRole() == AccountRole.USER) return Index.class;
		return null;
	}
	
	Object onActivate(long id){
		if(!userExists) return Login.class;
		if(user.getRole() == AccountRole.USER) return Index.class;
		currentUser = users.retriveSingleUser(id);
		return null;
	}
	
	Object onSuccess(){
		users.editSingleUser(currentUser);
		
		return ViewUser.class;
	}

}
